package com.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.pojo.Menus;
import com.erp.service.MenusService;
import com.erp.mapper.MenusMapper;
import com.erp.vo.MenusVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Teacher
 * @description 针对表【t_menus】的数据库操作Service实现
 * @createDate 2025-05-13 14:33:13
 */
@Service
public class MenusServiceImpl extends ServiceImpl<MenusMapper, Menus>
    implements MenusService{

    //注入MenusMapper
    @Autowired
    private MenusMapper menusMapper;
    @Override
    public List<MenusVo> queryMenusListService() {
//查询所有菜单节点数据
        List<Menus> menus = menusMapper.selectList(null);
//将menus集合转化为List<MenusVo>
        return doListMenus(menus,0);
    }

    private List<MenusVo> doListMenus(List<Menus> menus,Integer id){

//创建集合对象保存返回值
        List<MenusVo> result=new ArrayList<>();
//遍历menus集合获得每个菜单节点对象，m每个菜单节点对象
        for(Menus m:menus){
//m菜单节点对象的父节点id，是否和传入id相等，如果相等说明当前遍历的节点m，是id对应的菜单节点的子节点
            if(m.getPid().equals(id)){

                MenusVo menusVo=new MenusVo();
                BeanUtils.copyProperties(m,menusVo);
//进行递归遍历
                menusVo.setSubMenus(doListMenus(menus,m.getId()));
                result.add(menusVo);
            }
        }
        return result;
    }
    @Override
    public void saveMenusService(Menus menus) {


        QueryWrapper<Menus> wrapper=new QueryWrapper<>();
        wrapper.select("max(component) maxv");
//获得component的最大值
        Menus ms = menusMapper.selectOne(wrapper);

//component组件属性的值，是数据库最大值加1
        menus.setComponent(String.valueOf(ms.getMaxv()+1));
        menusMapper.insert(menus);

    }
}




